package com.digitalchina.lxnsy.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.interfaces.IBaseTemplate;
import com.digitalchina.lxnsy.sy.login.LoginActivity;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;

import butterknife.ButterKnife;
import wendu.dsbridge.DWebView;

/**
 * ================================================
 * 项目名称：bigdata
 * 类描述：
 * 包名：com.digitalchina.bigdata.base
 * 创建人：白彦东
 * 创建时间：2016/10/11 16:58
 * 修改备注：
 * ================================================
 */

public abstract class BaseV4Fragment extends Fragment implements IBaseTemplate {

    // F-Failed S-Success
    protected final int MSG_F_0 = 1001;
    protected final int MSG_S_0 = 1002;
    protected final int MSG_F_1 = 1003;
    protected final int MSG_S_1 = 1004;
    protected final int MSG_F_2 = 1005;
    protected final int MSG_S_2 = 1006;
    protected final int MSG_F_3 = 1007;
    protected final int MSG_S_3 = 1008;
    protected final int MSG_F_4 = 1009;
    protected final int MSG_S_4 = 1010;

    public View mRootView, mMainView;

    protected ViewGroup mContent;
    // 标题
    protected TextView mTextViewTitle;
    // 左侧按钮
    protected ImageView mIvBack;
    // 右侧图片按钮 mIvRight在最右边 mIvRight2在mIvRight的左边
    protected ImageView mIvRight, mIvRight2;
    // 右侧文字按钮
    protected TextView mTvRight;
    // 背景relativeLayout
    protected RelativeLayout mRelativeLayout;

    protected Handler mHandler;

    protected HttpParams params;

    protected LinearLayout mLlActionBar;

    // 加载框
    protected ProgressDialog dialog;

    // hybrid
    protected DWebView baseDWebView;

    public abstract View setRootView();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMainView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_base, container, false);
        mContent = (ViewGroup) mMainView.findViewById(R.id.fragment_common_rl_content);
        mLlActionBar = (LinearLayout) mMainView.findViewById(R.id.layout_fragment_actionbar);
        mRelativeLayout = (RelativeLayout) mMainView.findViewById(R.id.actionbar_relativeLayout);
        mRootView = setRootView();
        mContent.addView(mRootView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        dealSequence();
        return mMainView;
    }

    /**
     * 针对主页5个fragment适用
     */
    protected void setHomeFragmentTitleBar() {
        showActionBar();
        mRelativeLayout.setBackgroundResource(R.color.app_color);
        mTextViewTitle.setTextColor(getResources().getColor(R.color.colorWhite));
        mTvRight.setTextColor(getResources().getColor(R.color.colorWhite));
    }

    protected View inflate(int layoutId) {
        View view = LayoutInflater.from(getActivity()).inflate(layoutId, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 百度统计
    }

    @Override
    public void onPause() {
        super.onPause();
        // 百度统计
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void dealSequence() {
        setHandler();
        setLayout();
        initMember();
        addListener();
    }


    @Override
    public void setLayout() {
        mTextViewTitle = (TextView) mMainView.findViewById(R.id.actionbar_tv_title);
        mIvBack = (ImageView) mMainView.findViewById(R.id.actionbar_iv_back);
        mIvRight = (ImageView) mMainView.findViewById(R.id.actionbar_iv_right);
        mIvRight2 = (ImageView) mMainView.findViewById(R.id.actionbar_iv_right2);
        mTvRight = (TextView) mMainView.findViewById(R.id.actionbar_tv_right);

        baseDWebView = (DWebView) mMainView.findViewById(R.id.base_dweb_view);

        baseDWebView.addJavascriptObject(new JsApiApp(), "app");

        com.tencent.smtt.sdk.WebSettings webSettings=baseDWebView.getSettings();
        //允许webview对文件的操作
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        //用于js调用Android
        webSettings.setJavaScriptEnabled(true);
        //设置编码方式
        webSettings.setDefaultTextEncodingName("utf-8");

        baseDWebView.loadUrl("file:///android_asset/html/base.html");
        // Fragment 默认隐藏左上角返回按钮
        mIvBack.setVisibility(View.GONE);

        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightOnClick();
            }
        });

        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightOnClick();
            }
        });

        mIvRight2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                right2OnClick();
            }
        });
    }

    protected void callJsMethod(String method) {
        baseDWebView.callHandler(method, new String[]{""});
    }

    protected void callJsMethod(String method, String[] agrs) {
        baseDWebView.callHandler(method, agrs);
    }

    public class JsApiApp {
    }

    /**
     * 右上角第二个按钮监听
     */
    private void right2OnClick() {
    }

    /**
     * 设置标题
     *
     * @param title
     */
    protected void setTitle(String title) {

        if (null != mTextViewTitle)
            mTextViewTitle.setText(title);

    }


    /**
     * 设置右上角按钮文本
     *
     * @param title
     */
    protected void setRightText(String title) {

        if (null != mTvRight) {
            mTvRight.setText(title);
            mTvRight.setVisibility(View.VISIBLE);
        }

    }


    /**
     * 设置右上角图标按钮
     *
     * @param resId
     */
    protected void setRightImage(int resId) {

        if (null != mIvRight) {
            mIvRight.setImageResource(resId);
            mIvRight.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置右上角图标按钮
     *
     * @param resId
     */
    protected void setRightImage2(int resId) {

        if (null != mIvRight2) {
            mIvRight2.setImageResource(resId);
            mIvRight2.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏ActionBar
     */
    protected void hideActionBar() {

        mLlActionBar.setVisibility(View.GONE);

    }

    /**
     * 显示actionBar
     */
    protected void showActionBar() {

        mLlActionBar.setVisibility(View.VISIBLE);
    }

    /**
     * 提示语
     *
     * @param strMsg
     */
    protected void showToast(String strMsg) {
        if (!getActivity().isFinishing())
            ToastUtil.showToast(getActivity(), strMsg);
    }

    /**
     * 显示加载框
     *
     * @param message
     */
    protected void showDialog(String message) {
        if (dialog == null)
            dialog = CustomDialog.progressDialog(getActivity(), message);
        if (dialog != null && !dialog.isShowing())
            dialog.show();
    }

    /**
     * 隐藏加载框
     */
    protected void dismissDialog() {
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();
    }

    /**
     * 判断登录，状态未登录跳转至登录页面
     */
    public void noLoginToSkip(Class ActivityClz) {
        if ("1".equals(UserXML.getLoginStatus(getActivity()))) {
            GotoUtil.gotoActivity(getActivity(), ActivityClz);
        } else {
            GotoUtil.gotoActivity(getActivity(), LoginActivity.class);
        }
    }


}
